import { gql } from '@apollo/client';
import { withRouter } from 'react-router';
import { withApollo, graphql } from '@apollo/client/react/hoc';
import { compose } from 'recompose';
import JobFields from './fieldsFrag';

export default compose(
	withApollo,
	withRouter,
	graphql(
		gql`
			{
				JobMany(sort: _ID_DESC, limit: 0) {
					_id
					targetUrl
					target {
						host
						hostname
					}
					assetId
				}
				AssetMany {
					_id
					securityStatus
					isStarted
				}
				BalanceOne {
					_id
					uid
					balance {
						domain {
							value
							total
						}
					}
				}
				UserOne {
					identified
					_id
				}
			}
		`,
		{
			props({ data, refetch }) {
				return {
					data,
					refetch,
				};
			},
			options() {
				return {
					errorPolicy: 'all',
				};
			},
		}
	),
	graphql(
		gql`
			mutation($ids: [MongoID]) {
				JobRemoveMany(filter: { _ids: $ids }) {
					numAffected
				}
			}
		`,
		{
			props({ mutate }) {
				return {
					jobRemove(ids) {
						return mutate({
							variables: {
								ids,
							},
						});
					},
				};
			},
		}
	),
	graphql(
		gql`
			mutation(
				$record: UpdateOneJobInput!
				$filter: FilterUpdateOneJobInput!
				$enableMonitor: Boolean
				$assetId: MongoID!
			) {
				JobUpdateOne(filter: $filter, record: $record) {
					recordId
					record {
						...JobFields
					}
				}
				AssetUpdateOne(
					enableMonitor: $enableMonitor
					filter: { _id: $assetId }
					record: {}
				) {
					recordId
				}
			}
			${JobFields}
		`,
		{
			props({ mutate }) {
				return {
					jobUpdateOne(record) {
						return mutate({
							variables: {
								assetId: record.assetId,
								enableMonitor: record.enableMonitor,
								filter: {
									_id: record._id,
								},
								record: {
									enableMonitor: record.enableMonitor,
									enableAlert: record.enableAlert,
								},
							},
							optimisticResponse: {
								__typename: 'Mutation',
								JobUpdateOne: {
									__typename: 'UpdateOneJobPayload',
									recordId: record._id,
									record: {
										__typename: 'Job',
										...record,
										enableMonitor: record.enableMonitor,
										enableAlert: record.enableAlert,
									},
								},
								AssetUpdateOne: {
									__typename: 'UpdateOneAssetPayload',
									recordId: record.assetId,
								},
							},
						});
					},
				};
			},
		}
	),
	//
	graphql(
		gql`
			mutation($record: UpdateOneJobInput!, $filter: FilterUpdateOneJobInput!) {
				JobUpdateOne(filter: $filter, record: $record) {
					recordId
					record {
						...JobFields
					}
				}
			}
			${JobFields}
		`,
		{
			props({ mutate }) {
				return {
					jobUpdateOne2(record) {
						return mutate({
							variables: {
								filter: {
									_id: record._id,
								},
								record: {
									note: record.note,
									sourceIp: record.sourceIp,
								},
							},
						});
					},
				};
			},
		}
	),
	//
	graphql(
		gql`
			mutation($record: CreateOneJobInput!) {
				JobCreateOne(record: $record) {
					recordId
					record {
						...JobFields
					}
				}
			}
			${JobFields}
		`,
		{
			props({ mutate }) {
				return {
					jobCreateOne(record) {
						return mutate({
							variables: {
								record,
							},
						});
					},
				};
			},
		}
	)
);
